<template>
  <div class="search-result">
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error" error-text="搜索失败！请重试">
      <van-cell v-for="(item, index) in list" :key="index" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search'
export default {
  name: 'SearchResult',
  components: {},
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      error: false
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    async onLoad () {
      try {
        const { data: res } = await getSearchResult({
          page: this.page,
          per_page: 10,
          q: this.searchText
        })
        this.list.push(...res.data.results)
        console.log(this.list)
        this.loading = false
        if (res.data.results.length > 0) {
          this.page++
        } else {
          this.finished = true
        }
      } catch (err) {
        this.error = true
        this.$toast.fail('获取搜索结果失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
